<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>03 时钟</title>
</head>

<body>
    <h1 id="show"></h1>
</body>
<script>

    var h1 = document.getElementById('show');
    function getTime() {

        var date = new Date();
        var year = date.getFullYear(); // 年
        var month = date.getMonth(); // 月
        var day = date.getDate(); // 日
        var hour = date.getHours(); // 时
        var mins = date.getMinutes(); // 分
        var sec = date.getSeconds(); // 秒

        function toDb(num) {
            if (num < 10) {
                return '0' + num;
            } else {
                return '' + num;
            }
        }

        console.log(`${year}-${toDb(month)}-${toDb(day)} ${toDb(hour)}:${toDb(mins)}:${toDb(sec)}`);
        h1.innerHTML = `${year}-${toDb(month)}-${toDb(day)} ${toDb(hour)}:${toDb(mins)}:${toDb(sec)}`;
    }

    setInterval(getTime);
</script>

</html>